#pragma once
#include "tcpSocket.h"
#include "threadPool.h"
#include "task.h"
#include<vector>
#include<unordered_map>
#include<stack>
#include<functional>
class TcpServer
{
private:
    TcpSocket listen_fd_;
    std::string ip_;
    int port_;
public:
    TcpServer(const std::string& ip,int port):ip_(ip),port_(port){}
    void Start()
    {
        if(listen_fd_.CreateSocket() == false)
            std::cout << "CreateSocket error" << std::endl;
        if(listen_fd_.Bind(ip_,port_) == false)
            std::cout << "Bind error" << std::endl;
        if(listen_fd_.Listen(5) == false)
            std::cout << "Listen error" << std::endl;

        ThreadPool<Task> tp;

        while(1)
        {
            TcpSocket dest_sock;
            std::string ip;
            int port;
            if(listen_fd_.Accept(dest_sock,ip,port) == false)
            {
                std::cout << "Accept error" << std::endl;
                continue;
            }
            std::cout << "Create Task" << std::endl;

            int fd = dest_sock.GetFD();
            std::cout << "Ready to handler" << std::endl;
            Task t;
            t.fd_ = fd;
            tp.Push(t);
        }
    }
};